#include "eeprom_drv.h"

/**
 * @desc  : EEPROM初始化
 * @date  : 2025-04-21
 * @author: AnRui
 * @note  :
 */
void EepromDrvInit(void) {
    I2cSoftDrvInit();
}

/**
 * @desc  :
 * @date  : 2025-04-19
 * @author: AnRui
 * @param : reg - {参数说明}:
 * @param : data - {参数说明}:
 * @param : len - {参数说明}:
 * @note  : 注意写入数据时 自能一页一页写 写完一页数据中间要间隔一段时间
 *          第一页  0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
 *          当前寄存器地址为0x03  第一页的剩余空间为5   8 - 3 / 8
 */
bool WriteEepromData(uint8_t reg, uint8_t* data, uint32_t len) {
    // reg 为寄存器地址 也是 当前数据储存的个数
    if ((reg + len > EEPROM_SIZE) || data == NULL) {
        return false;
    }
    I2cStatusCode_e status_code = I2C_RESULT_OK;
    uint32_t wait_send_data_len = len;
    uint8_t cur_reg_addr = reg;
    uint8_t* p_current_data = data;

    while (wait_send_data_len) {
        // 剩余空间
        uint8_t remaining_space = EEPROM_PAGE_SIZE - cur_reg_addr % EEPROM_PAGE_SIZE;
        // 剩余空间大于发送的数据长度
        if (remaining_space >= wait_send_data_len) {
            status_code = I2cSoftWrite(EEPROM_ADDR, cur_reg_addr, p_current_data, wait_send_data_len);
            if (status_code != I2C_RESULT_OK) {
                return false;
            }
            wait_send_data_len = 0;
        } else {
            status_code = I2cSoftWrite(EEPROM_ADDR, cur_reg_addr, p_current_data, remaining_space);
            if (status_code != I2C_RESULT_OK) {
                return false;
            }
            // 待发送的数据个数
            wait_send_data_len -= remaining_space;
            // 下一次要发送的寄存器地址
            cur_reg_addr += remaining_space;
            // 下一次要发送的数据地址
            p_current_data += remaining_space;

        }
        EEPROM_TIME_SPACE();
    }
    return true;
}

/**
 * @desc  : 读取EEPROM数据
 * @date  : 2025-04-19
 * @author: AnRui
 * @param : reg - {参数说明}:
 * @param : data - {参数说明}:
 * @param : len - {参数说明}:
 * @return:
 * @note  :
 */
bool ReadEepromData(uint8_t reg, uint8_t* data, uint32_t len) {
    I2cStatusCode_e status_code = I2C_RESULT_OK;
    if ((reg + len > EEPROM_SIZE) || data == NULL) {
        // status_code = OTHER_ERR;
        return false;
    }
    status_code = I2cSoftRead((EEPROM_ADDR), reg, data, len);
    if (status_code != I2C_RESULT_OK) {
        return false;
    }
    return true;
}


/**
 * @desc  : EEPROM测试代码
 * @date  : 2025-04-21
 * @author: AnRui
 * @note  :
 */
void EepromTest(void) {
    uint8_t bufferWrite[EEPROM_SIZE];
    uint8_t bufferRead[EEPROM_SIZE];
    bool status_code = true;
    printf("AT24C02 writing data:\n");
    for (uint16_t i = 0; i < EEPROM_SIZE; i++) {
        bufferWrite[i] = i + 1;
        //        printf("0x%02X ", bufferWrite[i]);
    }
    status_code = WriteEepromData(0, bufferWrite, EEPROM_SIZE);
    if (status_code != true) {
        printf("AT24C02 write error,error code=%d,please check!\n", status_code);
        return;
    }
    DelayNms(5);

    printf("AT24C02 reading...\n");
    status_code = ReadEepromData(0, bufferRead, EEPROM_SIZE);
    if (status_code != true) {
        printf("AT24C02 read error,code %d,please check!\n", status_code);
        return;
    }
    for (uint16_t i = 0; i < EEPROM_SIZE; i++) {
        if (bufferRead[i] != bufferWrite[i]) {
            printf("0x%02X ", bufferRead[i]);
            printf("AT24C02 error,please check!\n");
            return;
        }
        printf("0x%02X ", bufferRead[i]);

    }
    printf("\n24C02 test successful!\n");
}
